import 'package:flutter/material.dart';

class DateTimePage extends StatefulWidget {
  const DateTimePage({Key? key}) : super(key: key);

  @override
  _DateTimePageState createState() => _DateTimePageState();
}

class _DateTimePageState extends State<DateTimePage> {
  late DateTime _dateTime = DateTime.now();
  late TimeOfDay _time = TimeOfDay.now();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("日期-时间-选择"),
        centerTitle: true,
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            InkWell(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text(
                      "日期：${_dateTime.year}-${_dateTime.month}-${_dateTime.day}"),
                  const Icon(Icons.arrow_drop_down)
                ],
              ),
              onTap: () async {
                DateTime? dateTime = await showDatePicker(
                    context: context,
                    initialDate: _dateTime,
                    firstDate: DateTime(1990, 1, 1),
                    lastDate: DateTime(2035, 1, 1));

                if (dateTime != null) {
                  setState(() {
                    _dateTime = dateTime;
                  });
                }
              },
            ),
            const SizedBox(height: 40),
            InkWell(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text("时间：${_time.hour}-${_time.minute}"),
                  const Icon(Icons.arrow_drop_down)
                ],
              ),
              onTap: () async {
                TimeOfDay? time =
                    await showTimePicker(context: context, initialTime: _time);

                setState(() {
                  _time = time!;
                });
              },
            ),
          ],
        ),
      ),
    );
  }
}
